package com.chp.controller;

import com.chp.common.util.PageResult;
import com.chp.common.util.R;
import com.chp.entity.Product;
import com.chp.service.IProductService;
import com.chp.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chp
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @RequestMapping("/list")
    public R list() {
        return R.ok().data("list",productService.list());
    }
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Product> pr = productService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }
    // 条件带分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) ProductVO vo){
        PageResult<Product> pr = productService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }


    // 添加学生
    @PostMapping("/add")
    public R add(@RequestBody Product student){
        boolean flag = productService.add(student);
        return flag ? R.ok().message("添加货物成功!") : R.error().message("添加货物失败!");
    }

    //修改学生
    @PutMapping("/update")
    public R update(@RequestBody Product student){
        boolean flag = productService.updateById(student);
        return flag ? R.ok().message("修改货物成功!") : R.error().message("修改货物失败!");
    }


    // 删除学生
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return productService.removeByIds(sids) ? R.ok().message("删除货物成功!") : R.error().message("删除货物失败!");
    }

}
